import axios from 'axios';
import qs from 'qs';
import { API_ROOT } from '../config';
import Vue from 'vue';
import Vuex from "vuex";

Vue.use(Vuex);

// axios.defaults.baseURL = 'http://192.168.172.107:9100/';
// axios.defaults.baseURL = 'http://192.168.172.113:9100/';
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*/*';
axios.defaults.withCredentials = true;

export default {
    //get请求
    get(url, param) {
        let mdParam = qs.parse(sessionStorage.getItem(Vue.prototype.route.path + '/mdParam'));
        let params = {
            ...param,
            ...mdParam
        };
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url: API_ROOT + url,
                params: params
            }).then(res => {
                resolve(res.data)
            }).catch((err) => {
                reject(err.response.status);
            });
        });
    },
    //post请求
    post(url, param, config) {
        let mdParam = qs.parse(sessionStorage.getItem(Vue.prototype.route.path + '/mdParam'));
        let params = {
            ...param,
            ...mdParam
        };
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url: API_ROOT + url,
                data: params,
                config
            }).then(res => {
                resolve(res.data)
            }).catch((err) => {
                reject(err.response.status);
            });
        });
    },
    formPost(url, params) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url: API_ROOT + url,
                params: params,
                headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
            }).then(res => {
                resolve(res.data);
            }).catch(res => {
                reject(err.response.status);
            });
        });
    },
    baseURL:API_ROOT
}
